﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CNVP.Framework;
using CNVP.JShop.UI;
using CNVP.Framework.Utils;

namespace CNVP.JShop.Web.Admin
{
    public partial class DeliveryWayEdit : AdminPage
    {
        BLL.CNVP_Column bll = new BLL.CNVP_Column();
        Model.CNVP_Column model = new Model.CNVP_Column();
        public int id = 0;

        #region _kuaidi
        public int _FirstPiece = 1;
        public decimal _FirstFreight=0;
        public int _AddPiece = 1;
        public decimal _AddFreight=0;
        public byte _IsDefault = 0;
        public string _ck = "";
        #endregion
        #region _EMS
        public int _FirstPiece_EMS = 1;
        public decimal _FirstFreight_EMS = 0;
        public int _AddPiece_EMS = 1;
        public decimal _AddFreight_EMS = 0;
        public byte _IsDefault_EMS = 0;
        public string _ck_EMS = "";
        #endregion
        #region _Pingyou
        public int _FirstPiece_Pingyou = 1;
        public decimal _FirstFreight_Pingyou = 0;
        public int _AddPiece_Pingyou = 1;
        public decimal _AddFreight_Pingyou = 0;
        public byte _IsDefault_Pingyou = 0;
        public string _ck_Pingyou = "";
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!BLL.BLLCheckAdmin.LoginOk(Page, 109)) return;//判断页面权限
            if (!string.IsNullOrEmpty(Request.QueryString["ID"]) && Public.IsNumber(Request.QueryString["ID"].ToString()))
            {
                id = Convert.ToInt32(Request.QueryString["ID"]);
            }
            if (!IsPostBack)
            {
                if (id > 0)
                {
                    lbTitle.Text = " > 修改";
                    DataInfo(id);
                }
                else
                {
                    lbTitle.Text = " > 添加";
                }
            }
        }

        public void DataInfo(int id)
        {
            model = bll.GetSingle_CNVP_ColumnById(id);
            if (model != null)
            {
                txtColumnName.Text = model.ColumnName;
                rblIsShow.SelectedValue = model.IsShow.ToString();
                txtOrderID.Text = model.OrderID.ToString();
                txtPicOldName.Text = model.ColumnUrl;
                if (model.ColumnUrl != "")
                {
                    imgPic.Visible = true;
                    imgPic.ImageUrl = "../UploadFile/File/" + model.ColumnUrl;
                }
                txtText.Text = model.Text;
                Liter_kuaidi.Text = DeliveryFreightAddRow(id, 1, "_Kuaidi");
                Liter_EMS.Text = DeliveryFreightAddRow(id, 2, "_EMS");
                Liter_Pingyou.Text = DeliveryFreightAddRow(id, 3, "_Pingyou");
            }
        }


        private string DeliveryFreightAddRow(int id, byte DeliveryType_Byte, string DeliveryType)
        {
            var AddTr = "";
            var list = new BLL.CNVP_DeliveryFreight().LoadAllByDeliveryFreightColumnID(id, DeliveryType_Byte, "");
            if (list != null && list.Count > 0)
            {

                for (int i = 0; i < list.Count; i++)
                {
                    if (list[i].IsDefault == 1)
                    {
                        if (DeliveryType == "_Kuaidi")
                        {
                            _FirstPiece = list[i].FirstPiece;
                            _FirstFreight = list[i].FirstFreight;
                            _AddPiece = list[i].AddPiece;
                            _AddFreight = list[i].AddFreight;
                            _ck = "checked=\"checked\"";

                        }
                        else if(DeliveryType == "_EMS")
                        {
                            _FirstPiece_EMS = list[i].FirstPiece;
                            _FirstFreight_EMS = list[i].FirstFreight;
                            _AddPiece_EMS = list[i].AddPiece;
                            _AddFreight_EMS = list[i].AddFreight;
                            _ck_EMS = "checked=\"checked\"";
                        }
                        else if(DeliveryType == "_Pingyou")
                        {
                            _FirstPiece_Pingyou = list[i].FirstPiece;
                            _FirstFreight_Pingyou = list[i].FirstFreight;
                            _AddPiece_Pingyou = list[i].AddPiece;
                            _AddFreight_Pingyou = list[i].AddFreight;
                            _ck_Pingyou = "checked=\"checked\"";
                        }

                    }
                    else
                    {
                        int len = list[i].ID;
                        AddTr += "<tr>";
                        AddTr += "	<td class=\"CheckBoxEdit\" >" + i + "</td>";
                        AddTr += "	<td class=\"CheckBoxEdit\">";
                        AddTr += "		<input id=\"CK" + DeliveryType + "\" name=\"CK" + DeliveryType + "\" type=\"checkbox\"/><input id=\"CKIsDefault" + DeliveryType + "\" name=\"CKIsDefault" + DeliveryType + "\" type=\"checkbox\" checked=\"checked\" value=\"0\" class =\"Display\"/></td>";
                        AddTr += "	<td>";
                        AddTr += "		<span id=\"ProvinceArray" + DeliveryType + "" + len + "\" >" + list[i].ProvinceArrayName + "</span>";
                        AddTr += "      <input id=\"ProvinceArrayName" + DeliveryType + "" + len + "\" name=\"ProvinceArrayName" + DeliveryType + "\" type=\"hidden\" value=\"" + list[i].ProvinceArrayName + "\" />";
                        AddTr += "		<input id=\"ProvinceArrayCode" + DeliveryType + "" + len + "\" name=\"ProvinceArrayCode" + DeliveryType + "\" type=\"hidden\" value=\"" + list[i].ProvinceArray + "\" />";
                        AddTr += "		<a id=\"ProvinceEidt" + DeliveryType + "" + len + "\" onclick=\"show_pl('" + DeliveryType + "','ProvinceArray" + DeliveryType + "" + len + "','ProvinceArrayName" + DeliveryType + "" + len + "','ProvinceArrayCode" + DeliveryType + "" + len + "')\"  href=\"javascript:void(0)\" target=\"_self\">编辑</a></td>";
                        AddTr += "	<td >";
                        AddTr += "		<input id=\"FirstPiece" + DeliveryType + "" + len + "\" type=\"text\" name=\"FirstPiece" + DeliveryType + "\" maxlength=\"20\" value=\"" + list[i].FirstPiece + "\"  /></td>";
                        AddTr += "	<td>";
                        AddTr += "		<input id=\"FirstFreight" + DeliveryType + "" + len + "\" type=\"text\" name=\"FirstFreight" + DeliveryType + "\" maxlength=\"20\" value=\"" + list[i].FirstFreight + "\" /></td>";
                        AddTr += "	<td >";
                        AddTr += "		<input id=\"AddPiece" + DeliveryType + "" + len + "\" type=\"text\" name=\"AddPiece" + DeliveryType + "\" maxlength=\"50\" maxlength=\"20\" value=\"" + list[i].AddPiece + "\"  /></td>";
                        AddTr += "	<td>";
                        AddTr += "		<input id=\"AddFreight" + DeliveryType + "" + len + "\" type=\"text\" name=\"AddFreight" + DeliveryType + "\" maxlength=\"20\" value=\"" + list[i].AddFreight + "\" /></td>";
                        AddTr += "</tr>";
                    }
                }
            }
            return AddTr;
        }

        protected void btUp_Click(object sender, EventArgs e)
        {
            if (txtColumnName.Text.Trim() == "")
            {
                BasePage.MessageBox("栏目名称不能为空！");
                txtColumnName.Focus();
                return;
            }
            if (id > 0)
            {
                string newName = "";
                string root = Server.MapPath("../UploadFile/File/");
                if (this.fileUp.PostedFile.ContentLength != 0)
                {
                    string fileName = fileUp.PostedFile.FileName;
                    string imgType = fileName.Substring(fileName.LastIndexOf(".") + 1);
                    if (imgType.ToLower() != "jpg" && imgType.ToLower() != "bmp" && imgType.ToLower() != "gif" && imgType.ToLower() != "jpeg" && imgType.ToLower() != "png")
                    {
                        BasePage.MessageBox("上传的图片必须为.jpg或.bmp或.gif或.jpeg或.png！");
                        return;
                    }
                    newName = "Delivery_" + DateTime.Now.ToString("yyyyMMddhhmmssfff") + "." + imgType;
                    fileUp.PostedFile.SaveAs(root + "\\" + newName);
                }
                else
                {
                    newName = txtPicOldName.Text;
                }
                int rows = new BLL.CNVP_Column().Update_CNVP_Column(
                       new Model.CNVP_Column()
                       {
                           ID = id,
                           ColumnName = txtColumnName.Text.Trim(),
                           ColumnType = (byte)4,
                           IsShow = byte.Parse(rblIsShow.SelectedValue),
                           OrderID = int.Parse(string.IsNullOrEmpty(txtOrderID.Text) ? "0" : txtOrderID.Text),
                           ColumnUrl = newName,
                           Depth = "0",
                           Text = txtText.Text.Trim()
                       }

               );
                if (rows > 0)
                {
                    new BLL.CNVP_DeliveryFreight().Delete_CNVP_DeliveryFreight(new Model.CNVP_DeliveryFreight()
                    {
                         DeliveryFreightColumnID=id
                    });
                    DeliveryFreightAdd(id, "_Kuaidi", 1, Request.Form["cb_Kuaidi"]);
                    DeliveryFreightAdd(id, "_EMS", 2, Request.Form["cb_EMS"]);
                    DeliveryFreightAdd(id, "_Pingyou", 3, Request.Form["cb_Pingyou"]);

                    if (this.fileUp.PostedFile.ContentLength != 0)
                    {
                        BasePage.DeleteFile(root + "\\" + txtPicOldName.Text.Trim());
                        imgPic.ImageUrl = "../UploadFile/File/" + newName;
                    }
                    txtPicOldName.Text = newName;
                    BasePage.MessageBox("修改成功！", "DeliveryWayList.aspx?Page=" + Request.QueryString["Page"] + "");
                }
                else
                { BasePage.MessageBox("修改失败！"); }
            }
            else
            {
                string newName = "";
                string root = Server.MapPath("../UploadFile/File/");
                if (this.fileUp.PostedFile.ContentLength != 0)
                {
                    string fileName = fileUp.PostedFile.FileName;
                    string imgType = fileName.Substring(fileName.LastIndexOf(".") + 1);
                    if (imgType.ToLower() != "jpg" && imgType.ToLower() != "bmp" && imgType.ToLower() != "gif" && imgType.ToLower() != "jpeg" && imgType.ToLower() != "png")
                    {
                        BasePage.MessageBox("上传的图片必须为.jpg或.bmp或.gif或.jpeg或.png！");
                        return;
                    }
                    newName = "Delivery_" + DateTime.Now.ToString("yyyyMMddhhmmssfff") + "." + imgType;
                    fileUp.PostedFile.SaveAs(root + "\\" + newName);
                }

                int rows = new BLL.CNVP_Column().Add_CNVP_Column(
                        new Model.CNVP_Column()
                        {
                            ColumnName = txtColumnName.Text.Trim(),
                            ColumnType = (byte)4,
                            IsShow = byte.Parse(rblIsShow.SelectedValue),
                            OrderID = int.Parse(string.IsNullOrEmpty(txtOrderID.Text) ? "0" : txtOrderID.Text),
                            ColumnUrl=newName,
                            Depth = "0",
                            Text = txtText.Text.Trim()
                        }
                );


                if (rows > 0)
                {

                    DeliveryFreightAdd(rows, "_Kuaidi", 1, Request.Form["cb_Kuaidi"]);
                    DeliveryFreightAdd(rows, "_EMS", 2, Request.Form["cb_EMS"]);
                    DeliveryFreightAdd(rows, "_Pingyou", 3, Request.Form["cb_Pingyou"]);
                    BasePage.MessageBox("添加成功！", "DeliveryWayList.aspx");
                }
                else
                { BasePage.MessageBox("添加失败！"); }
            }
        }

        private void DeliveryFreightAdd(int MaxID, string DeliveryType_Str, byte DeliveryType_Byte,string cb)
        {
            if (cb == "on")
            {
                string ProvinceArrayName_Str = "";
                string ProvinceArrayCode_Str = "";
                int FirstPiece_Int = 0;
                decimal FirstFreight_Decimal = 0;
                int AddPiece_Int = 0;
                decimal AddFreight_Decimal = 0;
                string[] CKIsDefault = Request.Form["CKIsDefault" + DeliveryType_Str + ""].Split(Convert.ToChar(","));
                for (int i = 0; i < CKIsDefault.Length; i++)//注意从‘0’开始
                {
                    string[] provincearrayname = Request.Form["ProvinceArrayName" + DeliveryType_Str + ""].Split(Convert.ToChar(","));
                    ProvinceArrayName_Str = Convert.ToString(provincearrayname[i]);
                    string[] provincearraycode = Request.Form["ProvinceArrayCode" + DeliveryType_Str + ""].Split(Convert.ToChar(","));
                    ProvinceArrayCode_Str = Convert.ToString(provincearraycode[i]);
                    string[] firstPiece = Request.Form["FirstPiece" + DeliveryType_Str + ""].Split(Convert.ToChar(","));
                    try
                    {
                        FirstPiece_Int = Convert.ToInt32(firstPiece[i]);
                    }
                    catch
                    { FirstPiece_Int = 1; }
                    string[] firstfreight = Request.Form["FirstFreight" + DeliveryType_Str + ""].Split(Convert.ToChar(","));
                    try
                    {
                        FirstFreight_Decimal = Convert.ToDecimal(firstfreight[i]);
                    }
                    catch
                    { FirstFreight_Decimal = 0; }
                    string[] addpiece = Request.Form["AddPiece" + DeliveryType_Str + ""].Split(Convert.ToChar(","));
                    try
                    {
                        AddPiece_Int = Convert.ToInt32(addpiece[i].ToString());
                    }
                    catch { AddPiece_Int = 1; }
                    string[] addfreight = Request.Form["AddFreight" + DeliveryType_Str + ""].Split(Convert.ToChar(","));
                    try
                    {
                        AddFreight_Decimal = Convert.ToDecimal(addfreight[i]);
                    }
                    catch { AddFreight_Decimal = 0; }

                    int DeliveryFreight_Int = new BLL.CNVP_DeliveryFreight().Add_CNVP_DeliveryFreight(new Model.CNVP_DeliveryFreight()
                    {
                        DeliveryFreightColumnID = MaxID,
                        DeliveryType = DeliveryType_Byte,
                        ProvinceArrayName=ProvinceArrayName_Str,
                        ProvinceArray = ProvinceArrayCode_Str,
                        FirstPiece = FirstPiece_Int,
                        FirstFreight = FirstFreight_Decimal,
                        AddPiece = AddPiece_Int,
                        AddFreight = AddFreight_Decimal,
                        IsDefault = Convert.ToByte(CKIsDefault[i])
                    });
                }
            }
        }
    }
}